package top.roadvast.cors.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableWebMvc
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        //每次调用registry.addMappin可以添加一个跨域配置，需要多个配置可以多次调用registry.addMapping
        registry.addMapping("/**")
                .allowedOrigins("*") //放行哪些原始域
                .allowedMethods("GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS") //放行哪些请求方式
                .allowCredentials(false) //是否发送 Cookie
                .allowedHeaders("*")
                .maxAge(3600);      //预请求缓存时间，单位秒
    }
}